package com.valuegroup.smarttodo.data.service;

import com.valuegroup.smarttodo.data.ServiceFactory;
import com.valuegroup.smarttodo.data.model.AppStatus;
import com.valuegroup.smarttodo.data.model.SumContent;
import com.valuegroup.smarttodo.data.model.SumContentDao;
import com.valuegroup.smarttodo.data.model.SumTopic;
import com.valuegroup.smarttodo.data.model.SumTopicDao;
import com.valuegroup.smarttodo.data.model.Summarize;
import com.valuegroup.smarttodo.data.model.Tag;
import com.valuegroup.smarttodo.data.model.TagDao;
import com.valuegroup.smarttodo.data.vo.SumContentTopicVo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class SumContentService extends BaseService {

    public SumContent getDefaultSumContent(Long sumId, Long topicId) {
        SumContent t = new SumContent();
        t.setContent("");
        t.setSumId(sumId);
        t.setTopicId(topicId);
        t.setCreatedDt(new Date());
        t.setUpdatedDt(t.getCreatedDt());
        t.setStatus(AppStatus.Status.NORMAL);
        return t;
    }

    public void updateContent(Long id, String content) {
        SumContent t = getSumContentDao().load(id);
        if (t == null) {
            return;
        }

        t.setContent(content);
        t.setUpdatedDt(new Date());
        getSumContentDao().update(t);
    }

    public void multiAddSumContent(List<Long> topicIds, Long sumId) {
        Summarize sum = getSummarizeDao().load(sumId);
        if (sum == null) {
            return;
        }
        for (Long topic : topicIds) {
            SumContent t = getDefaultSumContent(sumId, topic);
            getSumContentDao().save(t);
        }
    }

    public List<SumContentTopicVo> findSumContent(Long sumId) {
        List<SumContentTopicVo> result = new ArrayList<>();

        List<SumContent> list = getSumContentDao().queryBuilder()
                .where(SumContentDao.Properties.SumId.eq(sumId))
                .where(SumContentDao.Properties.Status.eq(AppStatus.Status.NORMAL))
                .list();

        List<SumTopic> topics = ServiceFactory.getInstance().getSumTopicService().findSumTopic();
        for (SumTopic topic : topics) {
            for (SumContent c : list) {
                if (c.getTopicId().equals(topic.getId())) {
                    SumContentTopicVo vo = new SumContentTopicVo();

                    vo.setTopicId(topic.getId());
                    vo.setTopicTitle(topic.getTitle());
                    vo.setContent(c.getContent());
                    vo.setCreatedDt(c.getCreatedDt());
                    vo.setUpdatedDt(c.getUpdatedDt());
                    vo.setId(c.getId());
                    vo.setSumId(c.getSumId());

                    result.add(vo);
                }
            }
        }
        return result;
    }

    public List<Long> findChoiceTopic(Long sumId) {
        List<Long> result = new ArrayList<>();
        List<SumContent> list = getSumContentDao().queryBuilder()
                .where(SumContentDao.Properties.SumId.eq(sumId))
                .list();

        for (SumContent c : list) {
            result.add(c.getTopicId());
        }
        return result;
    }

    public void delTopic(Long sumId, Long topicId) {
        List<SumContent> list = getSumContentDao().queryBuilder()
                .where(SumContentDao.Properties.SumId.eq(sumId))
                .where(SumContentDao.Properties.TopicId.eq(topicId))
                .where(SumContentDao.Properties.Status.eq(AppStatus.Status.NORMAL))
                .list();
        getSumContentDao().deleteInTx(list);
    }

    public boolean existRelate(Long topicId) {
        // 查找主题是否与业务数据有关联
        int count = existRelateToList(topicId).size();
        return count > 0;
    }

    public List<SumContent> existRelateToList(Long topicId) {
        return getSumContentDao().queryBuilder()
                .where(SumContentDao.Properties.TopicId.eq(topicId))
                .where(SumContentDao.Properties.Status.eq(AppStatus.Status.NORMAL))
                .list();
    }

    public void delOrDisableTopic(Long topicId, Long sumId) {
        List<SumContent> exist = existRelateToList(topicId);
        if (exist.size() > 0) {
            // 存在关联则先把当前总结中所关联的主题移除
            List<Long> delIds = new ArrayList<>();
            for (SumContent sc : exist) {
                if (sc.getSumId().equals(sumId)) {
                    delIds.add(sc.getId());
                }
            }
            getSumContentDao().deleteByKeyInTx(delIds);

            // 如还存在其它关联则设置成不可用状态一遍查询使用
            if (existRelate(topicId)) {
                SumTopic topic = getSumTopicDao().load(topicId);
                topic.setUsable(false);
                topic.setUpdatedDt(new Date());
                getSumTopicDao().update(topic);
            } else {
                getSumTopicDao().deleteByKey(topicId);
            }
        } else {
            // 无任何关联则直接删除
            getSumTopicDao().deleteByKey(topicId);
        }
    }

    public void addSumTopicAndBind(String text, Long sumId) {
        SumTopic topic = ServiceFactory.getInstance().getSumTopicService().addTopic(text);

        // 保存关联关系
        SumContent t = getDefaultSumContent(sumId, topic.getId());
        getSumContentDao().save(t);
    }

    public void clearTopic(Long sumId) {
        List<SumContent> list = getSumContentDao().queryBuilder()
                .where(SumContentDao.Properties.SumId.eq(sumId))
                .where(SumContentDao.Properties.Status.eq(AppStatus.Status.NORMAL))
                .list();
        getSumContentDao().deleteInTx(list);
    }
}
